package simulator.software.scheduling;

import java.util.List;

import simulator.software.Process;
import simulator.software.ProcessState;

/**
 * Implementation of the round robin algorithm for process scheduling.
 * 
 * @author Kyle Loomis
 * 
 */
public class RoundRobinScheduler implements Scheduler
{
   private Process lastRan = null;

   public String getDescription()
   {
      return "Round Robin Scheduler";
   }

   public Process nextProcess(List<Process> processTable)
   {
      Process next = null;

      int index = 0;

      if (lastRan != null)
      {
         index = processTable.indexOf(lastRan) + 1;
      }

      for (int i = index; i < processTable.size() && next == null; i++)
      {
         if (processTable.get(i).getState() == ProcessState.READY)
         {
            next = processTable.get(i);
         }
      }

      for (int i = 0; i < index && next == null; i++)
      {
         if (processTable.get(i).getState() == ProcessState.READY)
         {
            next = processTable.get(i);
         }
      }

      lastRan = next;

      return next;
   }

}
